﻿$(function () {
    $("#btnAdd").click(function () {
        var bid = $(this).attr("bookid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=add&bookid=" + bid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ardata = data.split("|");
                //thanh cong
                if (ardata[0] == "0") {
                    //cap nhat vao trang thai don hang
                    $("#lbBasket").html("[" + ardata[1] + "]" + " Giỏ sách của bạn");
                    $().toastmessage('showSuccessToast', "Thêm sách thành công");

                    //$().toastmessage('showNoticeToast', 'some message here');
                    //$().toastmessage('showSuccessToast', "some message here");
                    //$().toastmessage('showWarningToast', "some message here");
                    //$().toastmessage('showErrorToast', "some message here");
                    return;
                } else if (ardata[0] == "1") {
                    //khong du dieu kien de dat doc tiep
                    $().toastmessage('showErrorToast', ardata[1]);
                } else if (ardata[0] == "-1") {
                    //chua logon
                    window.location.href = "/vi/" + ardata[1] + "/dang-nhap";// + ardata[1];

                } else if (ardata[0] == "-2") {
                    //ve trang dat doc
                    window.location.href = ardata[1];
                }
            }
        });
    });

    $("#lbBasket").click(function () {
        window.location.href = "/InputOrderView.html";
    });

    $(".delImg").click(function () {
        var bookid = $(this).attr("bookid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=del&bookid=" + bookid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                //delete one book successfull
                if (data == "0") {
                    //remove current row
                    var rowid = "#row_" + bookid;
                    $(rowid).remove();
                    //reload cartCount
                    cartCount();
                }

            }
        });
    });

    $(".delImgOrder").click(function () {
        var orderid = $(this).attr("orderid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=delOrder&orderid=" + orderid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ar = data.split("|");
                //delete one book successfull
                if (ar[0] == "0") {
                    //remove current row
                    var rowid = "#row_" + orderid;
                    $(rowid).remove();

                } else {
                    $().toastmessage('showErrorToast', ar[1]);
                }

            }
        });
    });


    $(".delImgDetail").click(function () {
        var orderdetailid = $(this).attr("orderdetailid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=delOrderDetail&orderdetailid=" + orderdetailid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ar = data.split("|");
                //delete one book successfull
                if (ar[0] == "0") {
                    //remove current row
                    var rowid = "#row_" + orderdetailid;
                    $(rowid).remove();

                } else if (ar[0] == "-1") {
                    window.location.href = "/vi/" + ardata[1] + "/dang-nhap";// + ardata[1];
                } else {
                    $().toastmessage('showErrorToast', ar[1]);
                }

            }
        });
    });

    $("#btnBack").click(function () {
        window.location.href = "/Default.aspx";
    });

    //dem so luong book trong gio hang
    cartCount();

    //favo.start
    $("#btnFavourite").click(function () {
        var bid = $(this).attr("bookid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=addfavo&bookid=" + bid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ardata = data.split("|");
                //thanh cong
                if (ardata[0] == "0") {
                    $().toastmessage('showSuccessToast', "Thêm vào danh sách yêu thích thành công");
                    return;
                } else if (ardata[0] == "1") {
                    //da ton tai trong danh sach yeu thich cua ban
                    $().toastmessage('showErrorToast', ardata[1]);
                } else if (ardata[0] == "-1") {
                    //chua logon
                    window.location.href = "/vi/" + ardata[1] + "/dang-nhap";// + ardata[1];

                }
            }
        });
    });

    //dat hang tu danh sach favorite
    $(".favoAdd").click(function () {
        var favoid = $(this).attr("favoid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=favoadd2order&favoid=" + favoid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ardata = data.split("|");
                //thanh cong
                if (ardata[0] == "0") {
                    $().toastmessage('showSuccessToast', "Thêm vào danh sách yêu thích thành công");
                    return;
                } else if (ardata[0] == "1") {
                    //da ton tai trong danh sach yeu thich cua ban
                    $().toastmessage('showErrorToast', ardata[1]);
                } else if (ardata[0] == "-1") {
                    //chua logon
                    window.location.href = "/vi/" + ardata[1] + "/dang-nhap";// + ardata[1];

                }

            }
        });
    });

    //xoa favorite
    $(".favoDel").click(function () {
        var favoid = $(this).attr("favoid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=favodel&favoid=" + favoid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ar = data.split("|");

                var ardata = data.split("|");
                //thanh cong
                if (ardata[0] == "0") {
                    //remove current row
                    var rowid = "#row_" + favoid;
                    $(rowid).remove();
                } else if (ardata[0] == "1") {
                    //da ton tai trong danh sach yeu thich cua ban
                    $().toastmessage('showErrorToast', ardata[1]);
                } else if (ardata[0] == "-1") {
                    //chua logon
                    window.location.href = "/vi/" + ardata[1] + "/dang-nhap";// + ardata[1];

                }
            }
        });
    });

    //favo.end

    //extend order

    $("input[act=extend]").click(function () {
        var me = $(this);
        console.log(me);
        console.log(me.attr("orderid"));
        var orderid = me.attr("orderid");
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=orderextend&orderid=" + orderid,
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                var ardata = data.split("|");
                //thanh cong
                if (ardata[0] == "0") {
                    $().toastmessage('showSuccessToast', "Gia hạn đơn hàng thành công");
                    //an nut gia han
                    me.hide();
                    return;
                } else if (ardata[0] == "-1") {
                    //chua logon
                    window.location.href = "/vi/dang-nhap";// + ardata[1];
                } else{
                    //khong du dieu kien de dat doc tiep
                    $().toastmessage('showErrorToast', ardata[1]);
                }
            }
        });

    });

    //end extend order
});

function cartCount() {
    try {
        var strUrl = "/Handler/InputOrder.aspx";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: "task=count",
            dataType: "html",
            cache: true,
            beforeSend: function () {
            },
            success: function (data) {
                if (data != "") {
                    $("#lbBasket").html("[" + data + "]" + " Giỏ sách của bạn");
                }
            }
        });
    }
    catch (ex) {
        //   alert(ex);
    }
};